$("#head").load("header.html",function () {
})
$("#foot").load("footer.html",function () {})
new Vue({
    el:'#index',
    data:{
        //公用url
        url:'http://api.456mobi.com/upload/',
        //数据
        block:[],
        //切换按钮ID
        idlist:[],
        //图片链接
        imgData:'',
        //快讯保存
        newsFlash:[],
        //资讯保存
        information:[],
        //专访保存
        interview:[],
        //分析保存
        analysis:[],
        //轮播图数据
        carousel:{
            title : ''
        }
    },
    ready:function(){
        var that = this
//创建swiper
        setTimeout(function () {
            var mySwiper = new Swiper('.swiper-container', {
                loop: true,
                autoplay: true,
                autoplayDisableOnInteraction:false,
                on:{
                    slideChangeTransitionStart: function(e){
                        that.carousel.title = that.imgData.pic3[this.realIndex].title
                    }
                }
            });
        }, 150)
//图片接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/index/index',
            type:'get',
            success: function(res){
                if(res.code == '1'){
                    that.imgData = res.data
                    that.carousel.title = res.data.pic3[0].title
                }else{
                    alert('图片获取失败')
                }
            }
        })
//资讯接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/category',
            type:'get',
            success: function(ress){
                if(ress.code == '1'){
                    var res = ress
                    res.data.list.forEach(function (item,index) {
                        item.more = JSON.parse(item.more)
                        var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
                        if (isAndroid){
                            var time = new Date(item.published_time * 1000)
                            var Month = time.getMonth()
                            var Day = time.getDate()
                            var Hours = time.getHours()
                            var Minutes = time.getMinutes()
                            item.published_time = Month + '-' + Day + " " + Hours + ":" + Minutes
                        }else{
                            var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0]+' '+time[1][0]+':'+time[1][1]
                        }
                    })
                    that.block = res.data
                    for(var x in that.block.category){
                        that.idlist.push(that.block.category[x].id)
                        for(var c in that.block.category[x].child){
                            that.idlist.push(that.block.category[x].child[c].id)
                        }
                    }
                }else{
                    alert('资讯获取失败')
                }
            }
        })
//项目专访接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/index',
            type:'post',
            data:{
                category_id : 2
            },
            success: function(ress){
                var res = ress
                if(res.msg == '获取成功'){
                    res.data.forEach(function (item,index) {
                        item.more = JSON.parse(item.more)
                        var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
                        if (isAndroid){
                            var time = new Date(item.published_time * 1000)
                            var Month = time.getMonth()
                            var Day = time.getDate()
                            var Hours = time.getHours()
                            var Minutes = time.getMinutes()
                            item.published_time = Month + '-' + Day + " " + Hours + ":" + Minutes
                        }else{
                            var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0]+' '+time[1][0]+':'+time[1][1]
                        }
                        that.interview.push(item)
                    })
                }else{
                    // alert(res.msg)
                }
            }
        })
//行情分析接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/index',
            type:'post',
            data:{
                category_id : 3
            },
            success: function(ress){
                var res = ress
                if(res.msg == '获取成功'){
                    res.data.forEach(function (item,index) {
                        item.more = JSON.parse(item.more)
                        var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
                        if (isAndroid){
                            var time = new Date(item.published_time * 1000)
                            var Month = time.getMonth()
                            var Day = time.getDate()
                            var Hours = time.getHours()
                            var Minutes = time.getMinutes()
                            item.published_time = Month + '-' + Day + " " + Hours + ":" + Minutes
                        }else{
                            var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0]+' '+time[1][0]+':'+time[1][1]
                        }
                        that.analysis.push(item)
                    })
                }else{
                    // alert(res.msg)
                }
            }
        })
//点击切换事件
        $('.tabs>div').click(function () {
            if($('.tabs .active').html() == '资讯'){
                that.information = that.block.list
            }/*else if($('.tabs .active').html() == '专访'){
                that.interview = that.block.list
            }*/else{
                that.analysis = that.block.list
            }
            $('.tabs>div').removeClass('active')
            $(this).addClass('active')


            if($(this).html() == '资讯'){
                $('.tabs .arrow').css({'left':'10px'})
                that.block.list = that.information
            }/*else if($(this).html() == '专访'){
                $('.tabs .arrow').css({'left':'77px'})
                that.block.list = that.interview
            }*/else{
               // $('.tabs .arrow').css({'left':'144px'})
                $('.tabs .arrow').css({'left':'95px'})
                that.block.list = that.analysis
            }
        })
        var scr = function () {
            var winPos = $(window).scrollTop();
            if(winPos >= 400){
                $('#totop').css({'display':'inline-block'})
            }else{
                $('#totop').css({'display':'none'})
            }
        }
        $(window).scroll(function(){
            scr();
        })
        scr();
    },

     methods:{
        news_details:function (id,img_url) {
            $(location).attr('href', 'news_details.html?id='+id+'&img_url='+img_url);
        },
        more:function () {
            var that = this
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/index',
                type:'post',
                data:{
                    category_id : $('.tabs .active').attr("id"),
                    post_id : that.block.list[that.block.list.length-1].id
                },
                success: function(ress){
                    var res = ress
                    if(res.msg == '获取成功'){
                        res.data.forEach(function (item,index) {
                            item.more = JSON.parse(item.more)
                            var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
                            if (isAndroid){
                                var time = new Date(item.published_time * 1000)
                                var Month = time.getMonth()
                                var Day = time.getDate()
                                var Hours = time.getHours()
                                var Minutes = time.getMinutes()
                                item.published_time = Month + '-' + Day + " " + Hours + ":" + Minutes
                            }else{
                                var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                                time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                                time[1] = time[1].split(':')
                                item.published_time = time[0]+' '+time[1][0]+':'+time[1][1]
                            }
                            that.block.list.push(item)
                        })
                    }else if (res.msg=='暂无更多'){
                        $('.btn').css({'color':'#aaa','border':"2px solid #aaa"})
                        $('.btn').html('暂无更多')
                    }else{
                        // alert(res.msg)
                    }
                }
            })
        }
    }
});

